﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calculator
{
    class Calculator
    {
        public static double DoOperation(double num1, double num2, string op)
        {
            double result = double.NaN; 
            switch (op)
            {
                case "a":
                    result = num1 + num2;
                    break;
                case "s":
                    result = num1 - num2;
                    break;
                case "m":
                    result = num1 * num2;
                    break;
                case "d":
                    // 要求被除数非0
                    if (num2 != 0)
                    {
                        result = num1 / num2;
                    }
                    break;
                default:
                    break;
            }
            return result;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            bool endApp = false;
            //显示标题
            Console.WriteLine("C#四则运算计算器\r");
            Console.WriteLine("------------------------\n");

            while (!endApp)
            {
                //基本变量
                string numInput1 = "";
                string numInput2 = "";
                double result = 0;

                // 输入操作数1
                Console.Write("输入操作数1, 然后按下Enter: ");
                numInput1 = Console.ReadLine();

                double cleanNum1 = 0;
                while (!double.TryParse(numInput1, out cleanNum1))
                {
                    Console.Write("非有效输入，请输入一个数字: ");
                    numInput1 = Console.ReadLine();
                }

                // 输入操作数2
                Console.Write("输入操作数2, 然后按下Enter: ");
                numInput2 = Console.ReadLine();

                double cleanNum2 = 0;
                while (!double.TryParse(numInput2, out cleanNum2))
                {
                    Console.Write("非有效输入，请输入一个数字: ");
                    numInput2 = Console.ReadLine();
                }

                // Ask the user to choose an operator.
                Console.WriteLine("选择操作:");
                Console.WriteLine("\ta - 加");
                Console.WriteLine("\ts - 减");
                Console.WriteLine("\tm - 乘");
                Console.WriteLine("\td - 除");
                Console.Write("选择是? ");

                string op = Console.ReadLine();

                try
                {
                    result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
                    if (double.IsNaN(result))
                    {
                        Console.WriteLine("数学错误\n");
                    }
                    else Console.WriteLine("结果: {0:0.##}\n", result);
                }
                catch (Exception e)
                {
                    Console.WriteLine("一个意外发生了\n - 细节: " + e.Message);
                }

                Console.WriteLine("------------------------\n");

                // Wait for the user to respond before closing.
                Console.Write("按n退出，按其他键继续: ");
                if (Console.ReadLine() == "n") endApp = true;

                Console.WriteLine("\n"); // Friendly linespacing.
            }
            return;
        }
    }
}
